'''
注 次优惠劵只能用一次！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
输出商品表与excel同步！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
买一个少一个 ！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
'''


# import random
import time
import xlrd
import openpyxl


def rewtxl(num, vol):
    wb = openpyxl.load_workbook("shop.xlsx")
    worksheet = wb.worksheets[0]
    worksheet.cell(num, 3, int(vol - 1))
    wb.save(filename='shop.xlsx')


# 用户初始化自己余额：
money = input("请输入您的余额：")
money = int(money)
b = money

# 随机抽取一个优惠券
# coupon = [["洗衣机", 0.5], ["洗衣机", 0.5], ["微波炉", 0.7], ["微波炉", 0.7], ["风扇", 0.9], ["风扇", 0.9], ["风扇", 0.9],
#           ["iphone 13 plus", 0.85], ["iphone 13 plus", 0.85]]
# a = random.choice(coupon)
# print("你有一张", a[0], "的", a[1], "折扣卷")
# a = ["洗衣机", 0.5]  # 测试接口！！！！！！！！！！！！

# 就只优惠我的3090Ti！！！！！！！！！！！
a = ["3090Ti", 0.8]
print("OHHHHHHHHHHH 你有一张", a[0], "的", a[1], "折扣卷")
print("  商品          售价        数量")

myshopcar = []

while True:
    shop = []
    workbook = xlrd.open_workbook(r'shop.xlsx')
    sheet = workbook.sheet_by_index(0)
    for i in range(1, sheet.nrows):
        shop.append(sheet.row_values(i))
    for index, value in enumerate(shop):
        print(index, value)
    # 正常购物
    chose = input("请输入商品编号：")
    if chose == "q" or chose == "Q":
        print("欢迎下次光临！")
        break
    elif chose.isdigit():
        chose = int(chose)
        if chose >= len(shop):
            print("您输入商品编号不存在！")
        else:
            if (shop[chose][0] == a[0] and money < shop[chose][1] * a[1]) or (shop[chose][0] != a[0] and money < shop[chose][1]):
                print("穷鬼，钱不够！别瞎买！")
            else:
                if shop[chose][0] == a[0]:
                    money = money - shop[chose][1] * a[1]
                    myshopcar.append(shop[chose])
                    print("恭喜，商品添加成功！您的余额还剩", money)
                    a[1] = 1
                    rewtxl(chose+2, shop[chose][2])
                else:
                    money = money - shop[chose][1]
                    myshopcar.append(shop[chose])
                    print("恭喜，商品添加成功！您的余额还剩", money)
                    rewtxl(chose+2, shop[chose][2])
    else:
        print("非法输入！别瞎弄！")

# 打印购物小条
print("这是您的购物小条，请拿好：")
print("---------------------------")
list1 = []
sump = 0
for i in myshopcar:
    list1.append(i[0])
list1_set = set(list1)
for item in list1_set:
    for k in range(len(shop)):
        if shop[k][0] == item:
            price = shop[k][1]
            print("%s X %s------%.1f" % (item, list1.count(item), shop[k][1] * list1.count(item)))
            sump += shop[k][1] * list1.count(item)
print("\n")
print("应付￥", sump)
print("您实际花费了￥", round(b - money, 1))
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
print("---------------------------")
print("您带了", b, "￥进超市")
print("您的余额还剩:￥", money)